package com.lj.web.controller;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource; 

import net.sf.json.JSONArray;

import org.apache.commons.beanutils.converters.ByteArrayConverter;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;

import com.lj.web.bean.UserInfo;
import com.lj.web.business.UserInfoService;

@Controller
@RequestMapping("/user")
public class UserInfoController {
	@Resource(name="UserInfo")
	private UserInfoService userInfoService=null;
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	@RequestMapping(value="/getUserInfo.do",method=RequestMethod.POST)
	public @ResponseBody Map<String, Object> getUserInfo(@ModelAttribute UserInfo userInfo){
		 List<UserInfo> userList=null;
		 userList=userInfoService.selectUserInfo(userInfo);
		 JSONArray ja=JSONArray.fromObject(userList);
		 Map<String, Object> map=new HashMap<String, Object>();
		 map.put("total", userList==null?0:userList.size());
		 map.put("data", userList);
		return map;
	}
	@RequestMapping(value="/login.do",method=RequestMethod.POST)
	public @ResponseBody Map<String, Object> userLogin(@RequestParam("username") String name,@RequestParam("password") String pass){
		int resultType=0;
		String resultInfo="";
		Map<String, Object> map=new HashMap<String, Object>();
		if(name==null || "".equals(name) || pass==null || "".equals(pass)){
			resultType=1;
			resultInfo="用户名或密码不能为空";
		}else{
			UserInfo userInfo=new UserInfo();
			userInfo.setUserId(name);
			userInfo.setUserPass(pass);
			List<UserInfo> userList=null;
			userList=userInfoService.selectUserInfo(userInfo);
			if(userList==null || userList.size()<1){
				resultType=1;
				resultInfo="用户名或密码错误";
			}
		}
		map.put("result", resultType);
		map.put("info", resultInfo);
		return map;
	}
}
